/**
   communication_queue.h - Created by Timothy Morey on 8/11/2011.

   This file defines a queue that fascilitates cross-thread communication.  It is
   intended that each thread that needs to receive messages will have exactly one
   of queue, which will act as an inbox for messages.
 */

#ifndef __COMMUNICATION_QUEUE_H
#define __COMMUNICATION_QUEUE_H

#include <pthread.h>
#include <queue>

#include "message.h"

class CommunicationQueue
{
public:
  CommunicationQueue();
  ~CommunicationQueue();

public:

  /**
     Adds the specified message to the end of the queue.
   */
  void Push(MessagePtr msg);

  /**
     Adds the specified message to the end of the queue.  The memory associated
     with the Message will be managed by a boost shared_ptr.
  */
  void Push(Message* msg);

  /**
     Removes the first element in the queue and returns it.  If the queue is 
     empty, this will return a 0 pointer.

     @return The first element in the queue, or 0 if the queue is empty.
   */
  MessagePtr Pop();

  /**
     Indicates if the queue is empty without attaining a lock.
   */
  bool IsEmpty() const { return _Queue.empty(); }

protected:
  bool GetLock();
  void ReleaseLock();

protected:
  pthread_mutex_t _Mutex;
  std::queue<MessagePtr> _Queue;
};

#endif
